package com.viphome.controller;

import com.viphome.bean.Seckill;
import com.viphome.bean.dto.Exposer;
import com.viphome.bean.dto.SeckillExecution;
import com.viphome.bean.result.SeckillResult;
import com.viphome.enums.SeckillStatEnum;
import com.viphome.exception.RepeatKillException;
import com.viphome.exception.SeckillCloseException;
import com.viphome.servcice.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by wangzhanjin on 2017/12/27.
 * C控制层，跳转控制
 */
@Controller
@RequestMapping("/seckill")//url:/模块/资源/{id}/细分  /seckill/list
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    /**
     * 查看所有秒杀活动
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model){
        List<Seckill> list = seckillService.queryAll();
        model.addAttribute("list", list);
        return "list";
    }

    /**
     * 查看某个活动详情
     * @param seckillId
     * @param model
     * @return
     */
    @RequestMapping("/{seckillId}/detail")
    public String detail(@PathVariable("seckillId") Long seckillId,Model model){

        if (seckillId==null){
            //重定向
            return "redirect:/seckill/list";
        }
        Seckill seckill = seckillService.queryById(seckillId);
        if(seckill==null){
            //请求转发
            return "forward:/seckill/list";
        }
        model.addAttribute("seckill", seckill);
        return "detail";
    }

    /**
     * 秒杀地址返回
     * ajax接口，返回json, @ResponseBody
     * @param seckillId
     */
    @RequestMapping(value = "/{seckillId}/exposer",
            method = RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
//    public void/*todo*/ exporser(Long seckillId){
    public SeckillResult<Exposer> exporser(@PathVariable Long seckillId){
        SeckillResult<Exposer> result;
        try {
            Exposer exposer = seckillService.exportSeckillUrl(seckillId);
            result = new SeckillResult<Exposer>(true, exposer);
        } catch (Exception e) {
            e.printStackTrace();
            result = new SeckillResult<Exposer>(false, e.getMessage());
        }
        return result;
    }

    /**
     * 执行秒杀操作
     * @param seckillId
     * @param md5
     * @param phone
     * @return
     */
    @RequestMapping(value = "/{seckillId}/{md5}/execution",
                    method = RequestMethod.POST,
                    produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<SeckillExecution> excute(@PathVariable("seckillId") Long seckillId,
                                                  @PathVariable("md5") String md5,
                                                  @CookieValue(value="killPhone",required = false) Long phone){
        SeckillResult<SeckillExecution> result=null;
        if (phone==null){
            return new SeckillResult<SeckillExecution>(false, "未注册");
        }
        SeckillExecution seckillExecution;
        try {
            seckillExecution = seckillService.excuteSeckill(seckillId, phone, md5);
//            seckillExecution = seckillService.excuteSeckillProcedure(seckillId, phone, md5);
        } catch (RepeatKillException e2){
            seckillExecution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
        }catch (SeckillCloseException e3){
            seckillExecution = new SeckillExecution(seckillId, SeckillStatEnum.END);
        }catch (Exception e) {
            e.printStackTrace();
            seckillExecution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
        }
        return new SeckillResult<SeckillExecution>(true, seckillExecution);
    }

    /**
     * 查看系统时间
     * @return
     */
    @RequestMapping("/time/now")
    @ResponseBody
    public SeckillResult<Long> time(){
        Date now = new Date();
        return new SeckillResult(true,now.getTime());
    }
}
